Improve which-key-add-keymap-based-replacements.
authorJustin Burkett <justin@burkett.cc>
Tue, 22 Jun 2021 17:20:40 +0000 (13:20 -0400)
committerJustin Burkett <justin@burkett.cc>
Tue, 22 Jun 2021 17:20:40 +0000 (13:20 -0400)
Also, teach which-key--safe-lookup-key to handle numeric results.

which-key.el

index 8598fa666e1bb3f4b5cff3562991b2d8590617de..67b185f2c24f8c657b612403f155bdd0e27d65d0 100644 (file)
@@ -909,20 +909,16 @@ For backwards compatibility, REPLACEMENT can also be a string,
 but the above format is preferred, and the option to use a string
 for REPLACEMENT will eventually be removed."
   (while key
-    (cond ((consp replacement)
-           (define-key keymap (kbd key) replacement))
-          ((stringp replacement)
-           (let ((binding (lookup-key keymap (kbd key))))
-             (if (or (null binding)
-                     (numberp binding))
-                 ;; using a keymap in case someone intends to make this a
-                 ;; prefix. If they want to bind something else, they will just
-                 ;; end up overriding the prefix map
-                 (define-key keymap (kbd key)
-                   (cons replacement (make-sparse-keymap)))
-               (define-key keymap (kbd key) (cons replacement binding)))))
-          (t
-           (user-error "replacement is neither a cons cell or a string")))
+    (let ((def
+           (cond
+            ((consp replacement) replacement)
+            ((stringp replacement)
+             (cons replacement
+                   (or (which-key--safe-lookup-key keymap (kbd key))
+                       (make-sparse-keymap))))
+            (t
+             (user-error "replacement is neither a cons cell or a string")))))
+      (define-key keymap (kbd key) def))
     (setq key (pop more)
           replacement (pop more))))
 (put 'which-key-add-keymap-based-replacements 'lisp-indent-function 'defun)
@@ -1445,8 +1441,13 @@ local bindings coming first. Within these categories order using
   (if (stringp maybe-string) (string-width maybe-string) 0))
 
 (defsubst which-key--safe-lookup-key (keymap key)
-  "Version of `lookup-key' that allows KEYMAP to be nil. KEY is not checked."
-  (when (keymapp keymap) (lookup-key keymap key)))
+  "Version of `lookup-key' that allows KEYMAP to be nil.
+Also convert numeric results of `lookup-key' to nil. KEY is not
+checked."
+  (when (keymapp keymap)
+    (let ((result (lookup-key keymap key)))
+      (when (and result (not (numberp result)))
+        result))))
 
 (defsubst which-key--butlast-string (str)
   (mapconcat #'identity (butlast (split-string str)) " "))